/**
 * Created by CHENCO7 on 8/4/2017.
 */
function addToCart(btn) {
    shake();
    var parentNode = $(btn).parent().parent();
    var imgPath = parentNode.find('img').attr('title');
    var name = parentNode.find('.food-info-div h3').text();
    var price = parentNode.find('.food-info-div p').text();
    var carts = $("#cart ul");

    if (carts.length > 0) {
        for (var i = 0; i < carts.length; i++) {
            if ($(carts[i]).children("[class=food_name]").text() == name) {
                up($($(carts[i]).find("button")[1]));
                return;
            }
        }
    }
    var ul = $("<ul class='list-ul'></ul>");
    $("#cart").append(ul);
    ul.append("<li class='food_name'>" + name + "</li>");
    ul.append("<li>" + price + "</li>");
    ul.append("<li><button class='btn btn-default btn-xs' onclick='del($(this))'>-</button><span>1</span><button class='btn btn-default btn-xs' onclick='up($(this))'>+</button></li>");
    ul.append("<li style='display: none' title='" + imgPath + "'></li>");
    count();
}

function addToCartFromModal(btn) {
    shake();
    var formGroup = $(btn).parent().parent().find(".form-group");

    var imgPath = formGroup.eq(0).find('img').attr('title');
    var name = formGroup.eq(1).find("input[name=name]").val();
    var price = formGroup.eq(2).find("input[name=price]").val();

    var carts = $("#cart ul");

    if (carts.length > 0) {
        for (var i = 0; i < carts.length; i++) {
            if ($(carts[i]).children("[class=food_name]").text() == name) {
                up($($(carts[i]).find("button")[1]));
                return;
            }
        }
    }
    var ul = $("<ul class='list-ul'></ul>");
    $("#cart").append(ul);
    ul.append("<li class='food_name'>" + name + "</li>");
    ul.append("<li>" + price + "</li>");
    ul.append("<li><button onclick='del($(this))'>-</button><span>1</span><button onclick='up($(this))'>+</button></li>");
    ul.append("<li style='display: none' title='" + imgPath + "'></li>");
    count();
}

function shake() {
    $(".show-carticon-btn").animate({
        opacity: 1,
        left: "-=20px"
    }, 500);
    $(".show-carticon-btn").animate({
        opacity: 1,
        left: "+=20px"
    }, 500);
    $("#cart-body").animate({
        opacity: 1,
        right: "+=20px"
    }, 500);
    $("#cart-body").animate({
        opacity: 1,
        right: "-=20px"
    }, 500);
}


function up(node) {
    var number = node.last().parent().children("span").text();
    var result = parseInt(number) + 1;
    node.last().parent().children("span").text(result);
    count();
}

function del(node) {
    var number = node.last().parent().children("span").text();
    var result = parseInt(number) - 1;
    node.last().parent().children("span").text(result);
    count();
}

function count() {
    var carts = $("#cart ul");
    var sum = 0;
    for (var i = 0; i < carts.length; i++) {
        var name = $(carts[i]).children("li").eq(0).text();
        var price = $(carts[i]).children("li").eq(1).text();
        var amount = $(carts[i]).children("li").eq(2).children("span").text();
        var imgPath = $(carts[i]).children("li").eq(3).attr("title");
        var orderItem = {name: name, price: price, amount: amount, imgPath: imgPath};
        $.ajax({
            type: "POST",
            url: "addToOrderList",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(orderItem)
        }).done(function (data) {
            // console.log(data);
        });
        sum += parseInt(parseInt(price) * parseInt(amount));
    }

    $("#sum").text("总计： " + sum);
}

function getRemoteData(typeId) {
    $.ajax({
        type: "GET",
        url: "showFoodListData?typeId=" + typeId + "&m_id=" + $(".m_name").attr("id"),
        dataType: "json"
    }).done(function (data) {
        updateTable(data);
    })
}
function updateTable(data) {
    $("#tbodyId").empty();
    var tr = $("<tr></tr>");

    data.forEach(function (item) {
        var tr = $("<tr></tr>");
        $("#tbodyId").append(tr);
        tr.append('<td><a href="showFoodList?m_id=' + item.id + '&m_name=' + item.name + '"><img src="' + imgUrlPrefix + item.image + '" style="width:100px; height:100px"></a></td>');
        tr.append('<td><a href="showFoodList?m_id=' + item.id + '&m_name=' + item.name + '">' + item.name + '</a></td>');
        tr.append('<td>' + item.price + '</td>');
        tr.append('<td><button class="btn btn-default" onclick="addToCart($(this))">加入购物车</button></td>');
    });
}